﻿Imports Licenta3.Common.Classes
Imports Microsoft.Office.Interop.Word
Imports System.IO

Public Class Anexa2Class
    Inherits AnexaClass

    'properties
    Private _docCadastrala
    Public Property docCadastrala As String
        Get
            Return _docCadastrala
        End Get
        Set(value As String)
            'If TipDocCadastrala.values.Contains(value) Then
            _docCadastrala = value
            'End If
            OnPropertyChanged("docCadastrala")
        End Set
    End Property
    Private _TipInscriere As String
    Public Property TipInscriere As String
        Get
            Return _TipInscriere
        End Get
        Set(value As String)
            _TipInscriere = value
            OnPropertyChanged("TipInscriere")
        End Set
    End Property
    Private _ObiectInscriere As String
    Public Property ObiectInscriere As String
        Get
            Return _ObiectInscriere
        End Get
        Set(value As String)
            _ObiectInscriere = value
            OnPropertyChanged("ObiectInscriere")
        End Set
    End Property
    Private _ActJustificator As String
    Public Property ActJustificator As String
        Get
            Return _ActJustificator
        End Get
        Set(value As String)
            _ActJustificator = value
            OnPropertyChanged("ActJustificator")
        End Set
    End Property
    Private _RaspunsPrin As String
    Public Property RaspunsPrin As String
        Get
            Return _RaspunsPrin
        End Get
        Set(value As String)
            _RaspunsPrin = value
            OnPropertyChanged("RaspunsPrin")
        End Set
    End Property

    'methods
    Public Overrides Sub toWordOrPDF(Optional format As Microsoft.Office.Interop.Word.WdSaveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault, Optional printOnly As Boolean = False)
        Dim i As Integer
        'Dim currentDirectory As String
        Dim Word As New Application
        Dim AnexaDoc As New Document
        Dim sablon As Object = (assemblyPath & "\WordTemplates\" & Me.ShortName & ".dotx")
        AnexaDoc = Word.Documents.Add(sablon, False, WdNewDocumentType.wdNewBlankDocument, False)
        Dim currentDirectory = SetariClass._LucruPath & "\" & Me.Autorizat.Nume & "\" & Me.OCPI.denumire & "\" & Me.Imobil.UAT.Denumire & "\" & Me.proprietar.Nume
        If IO.Directory.Exists(currentDirectory) = False Then
            IO.Directory.CreateDirectory(currentDirectory)
        End If
        For i = AnexaDoc.ContentControls.Count To 1 Step -1
            With AnexaDoc.ContentControls.Item(i)
                Select Case .Tag
                    Case "OneForPF"
                        If Me.Autorizat.IsPF Then
                            .DropdownListEntries(1).Select()
                        Else
                            .DropdownListEntries(2).Select()
                        End If
                        'Continue For
                    Case "ProprietarIsPJ2bd"
                        If Me.proprietar.IsPF = False Then
                            .Delete(True)
                        End If
                End Select
                Select Case .Title
                    Case "OCPI"
                        .Range.Text = Me.OCPI.denumire.ToUpper
                        'Continue For
                    Case "BCPI"
                        .Range.Text = Me.BCPI.denumire.ToUpper
                        'Continue For
                    Case "NumeProprietar"
                        .Range.Text = Me.proprietar.Nume
                    Case "AdresaProprietar"
                        .Range.Text = Me.proprietar.adresa.adresaIntreaga
                    Case "SeriaCIProprietar"
                        .Range.Text = Me.proprietar.SerieCI
                    Case "NrCIProprietar"
                        .Range.Text = Me.proprietar.NrCI
                    Case "IDProprietar"
                        .Range.Text = Me.proprietar.id
                    Case "telProprietar"
                        .Range.Text = Me.proprietar.tel
                    Case "emailProprietar"
                        .Range.Text = Me.proprietar.email
                    Case "NumeMandatar"
                        .Range.Text = Me.Autorizat.Nume
                    Case "tipReceptie"
                        Select Case Me.docCadastrala
                            Case "Prima inscriere"
                                .DropdownListEntries(1).Select()
                            Case "Prima inscriereUI"
                                .DropdownListEntries(1).Select()
                            Case "Inscriere constructie definitiva"
                                .DropdownListEntries(2).Select()
                            Case "Modificarea limitei"
                                .DropdownListEntries(3).Select()
                            Case "Modificarea suprafetei"
                                .DropdownListEntries(4).Select()
                            Case "Descrierea dezmembramintelor"
                                .DropdownListEntries(5).Select()
                            Case "Reconstituire"
                                .DropdownListEntries(6).Select()
                        End Select
                    Case "tipInscriere"
                        Select Case Me.TipInscriere
                            Case "Întabulare"
                                .DropdownListEntries(1).Select()
                            Case "Înscriere Provizorie"
                                .DropdownListEntries(2).Select()
                            Case "Notare"
                                .DropdownListEntries(3).Select()
                            Case "Radiere"
                                .DropdownListEntries(4).Select()
                        End Select
                    Case "ObiectInscriere"
                        .Range.Text = Me.ObiectInscriere
                    Case "NrCF"
                        .Range.Text = Me.Imobil.NrCF
                    Case "NrCadastral"
                        .Range.Text = Me.Imobil.NrCadastral
                    Case "LocalitateImobil"
                        .Range.Text = Me.Imobil.UAT.Denumire
                    Case "AdresaImobil"
                        .Range.Text = Me.Imobil.Adresa.adresaIntreaga
                    Case "ActJustificator"
                        .Range.Text = Me.ActJustificator
                    Case "TipRaspuns"
                        Select Case Me.RaspunsPrin
                            Case "poștă"
                                .DropdownListEntries(1).Select()
                            Case "fax"
                                .DropdownListEntries(2).Select()
                            Case "biroul de cadastru"
                                .DropdownListEntries(3).Select()
                        End Select

                End Select
            End With
        Next
        'Dim format As Object = "wdFormatPDF"

        Dim ext As String = ".docx"
        If format = WdSaveFormat.wdFormatPDF Then ext = ".pdf"
        If printOnly = True Then
            Try
                'Word.Visible = True
                AnexaDoc.Activate()
                If File.Exists(currentDirectory & "\" & Me.ShortName & ".docx") = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                Else
                    File.Delete(currentDirectory & "\" & Me.ShortName & ".docx")
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ".docx")
                End If
                Word.PrintOut(FileName:=currentDirectory & "\" & Me.ShortName & ".docx")
                'System.Threading.Thread.Sleep(1000)
                'File.Delete(currentDirectory & "\" & Me.ShortName & ".docx")
            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source, MsgBoxStyle.Information)
            End Try
        Else
            Try
                If File.Exists(currentDirectory & "\" & Me.ShortName & "" & ext) = False Then
                    AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & "" & ext, format)
                Else
                    If MsgBox("Fisierul exista! Doriti sa-l suprascrieti?", MsgBoxStyle.YesNo, "Sunteti sigur?") = MsgBoxResult.Yes Then
                        File.Delete(currentDirectory & "\" & Me.ShortName & ext)
                        AnexaDoc.SaveAs2(currentDirectory & "\" & Me.ShortName & ext, format)
                    Else
                        MsgBox("Niciun fisier nu a fost modificat", MsgBoxStyle.OkOnly)
                    End If
                End If
            Catch ex As Exception
                MsgBox(ex.Message + vbCrLf + ex.Source + vbCrLf + ex.StackTrace, vbOKOnly)
            End Try
        End If

        AnexaDoc.Close(WdSaveOptions.wdDoNotSaveChanges)
        AnexaDoc = Nothing
        'AnexaDoc.SaveAs2(currentDirectory & "\ANEXA1.docx")
        'Word.Visible = True
        Word.Quit(WdSaveOptions.wdDoNotSaveChanges)
        Word = Nothing
    End Sub

    Public Sub New()
        Me.ShortName = "ANEXA2"
    End Sub
End Class
